﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Guids;
using Volo.Abp;
using Volo.Abp.Domain.Entities;

namespace XuZhou.SmartWaterConservancy.HeHu.Waters
{
    public class EngineeringStation : WaterStationBase
    {
        /// <summary>
        /// 闸门开度(%)
        /// </summary>
        public double? GateOpeningDegree { get; private set; }

        /// <summary>
        /// 水位(m)
        /// </summary>
        public double? WaterLevel { get; private set; }

        /// <summary>
        /// 闸门开闭状态
        /// </summary>
        public GateStatus GateStatus { get; private set; }

        /// <summary>
        /// 开启次数
        /// </summary>
        public int OpeningCount { get; private set; }

        /// <summary>
        /// 闸门操作记录
        /// </summary>
        public ICollection<GateOperationRecord> GateOperationRecords { get; private set; }

        protected EngineeringStation() : base()
        {
            GateOperationRecords = new List<GateOperationRecord>();
        }

        public EngineeringStation(
            Guid id,
            string code,
            string name,
            string administrativeDivisionCode,
            double longitude,
            double latitude,
            string remark = null)
            : base(id, code, name, WaterStationType.Engineering, administrativeDivisionCode, longitude, latitude, remark)
        {
            GateOperationRecords = new List<GateOperationRecord>();
            GateStatus = GateStatus.Closed;
            OpeningCount = 0;
        }

        /// <summary>
        /// 更新工情监测数据
        /// </summary>
        public void UpdateMonitoringData(double? gateOpeningDegree, double? waterLevel)
        {
            GateOpeningDegree = gateOpeningDegree;
            WaterLevel = waterLevel;
        }

        /// <summary>
        /// 打开闸门
        /// </summary>
        public void OpenGate()
        {
            //if (GateStatus == GateStatus.Open)
            //{
            //    throw new BusinessException("闸门已经处于开启状态");
            //}

            //GateStatus = GateStatus.Open;
            //OpeningCount++;

            //GateOperationRecords.Add(new GateOperationRecord(
            //    GuidGenerator.Create(),
            //    Id,
            //    GateOperationType.Open,
            //    DateTime.Now
            //));
        }

        /// <summary>
        /// 关闭闸门
        /// </summary>
        public void CloseGate()
        {
            //if (GateStatus == GateStatus.Closed)
            //{
            //    throw new BusinessException("闸门已经处于关闭状态");
            //}

            //GateStatus = GateStatus.Closed;
            
            

            //GateOperationRecords.Add(new GateOperationRecord(
            //    GuidGenerator.Create(),
            //    Id,
            //    GateOperationType.Close,
            //    DateTime.Now
            //));
        }


        /// <summary>
        /// 闸门操作类型
        /// </summary>
        public enum GateOperationType
        {
            /// <summary>
            /// 开启
            /// </summary>
            Open = 1,

            /// <summary>
            /// 关闭
            /// </summary>
            Close = 2
        }

        /// <summary>
        /// 闸门操作记录
        /// </summary>
        public class GateOperationRecord : Entity<Guid>
        {
            /// <summary>
            /// 站点ID
            /// </summary>
            public Guid StationId { get; private set; }

            /// <summary>
            /// 操作类型
            /// </summary>
            public GateOperationType OperationType { get; private set; }

            /// <summary>
            /// 操作时间
            /// </summary>
            public DateTime OperationTime { get; private set; }

            /// <summary>
            /// 运行时长
            /// </summary>
            public long RunTime { get; private set; }

            protected GateOperationRecord() { }

            public GateOperationRecord(Guid id, Guid stationId, GateOperationType operationType, DateTime operationTime)
                : base(id)
            {
                StationId = stationId;
                OperationType = operationType;
                OperationTime = operationTime;
            }
        }
    }
}
